// 验证用户名是否是否符合规则
interface ValidateRules {
    unEmpty: (username: string) => {status: boolean, msg: string | null};
    unallNumber: (username: string) => {status: boolean, msg: string | null};
    unLegitimateSybl: (username: string) => {status: boolean, msg: string | null};
}
export const validateUsername = (username: string, rules: string[]) => {
    // 过滤表情符号
    username =  username.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, '');
    const validateRes: Array<{status: boolean, msg: string | null}> = [];
    const validateRules: ValidateRules = {
        unEmpty(userStr: string) {
            const status = userStr.trim().length > 0;
            const res = {
                status,
                msg: status ? '' : '请输入用户名'
            };
            validateRes.push(res);
            return res;
        },
        unallNumber(userStr: string) {
            const status = /^\d+$/.test(userStr.trim());
            const res = {
                status: !status,
                msg: status ? '请勿设置纯数字用户名' : ''
            };
            validateRes.push(res);
            return res;
        },
        // 合法符号 非中英文非下划线的标点符号 emoji表情
        unLegitimateSybl(userStr: string) {
            // ^[a-zA-Z0-9_/u4e00-/u9fa5]+$
            const status = /^[(\u4e00-\u9fa5)\w]+$/.test(userStr.trim());
            const res = {
                status,
                msg: status ? '' : '请输入中英文、数字和下划线组合'
            };
            validateRes.push(res);
            return res;
        }
    };
    rules.length === 0 && (rules = Object.keys(validateRules));
    rules.forEach((item, index) => {
        if (validateRules[(item as keyof ValidateRules)]) {
            const fn = validateRules[(item as keyof ValidateRules)];
            fn(username);
        }
    });
    const failpass = validateRes!.find((item) => {
        return item.status === false;
    });
    return failpass;
};
